using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataMgr : MonoSingleton<DataMgr>, IManager
{
    private readonly string UserName_Key = "UserName";
    private readonly string UserPassword_Key = "UserPassword";
    private readonly string UserMusic_Key = "Music";
    private readonly string UserSound_Key = "Sound";

    private User player;
    private int music;
    private int sound;

    public User Player
    {
        get => player;
        set => player = value;
    }

    public bool MusicEnable
    {
        get => music == 1;
        set => music = value ? 1 : 0;
    }

    public bool SoundEnable
    {
        get => sound == 1;
        set => sound = value ? 1 : 0;
    }

    public void Init()
    {
        Read();
    }

    public void UnInit()
    {
        Write();
    }

    private void Read()
    {
        string name = PlayerPrefs.GetString(UserName_Key);
        string password = PlayerPrefs.GetString(UserPassword_Key);
        player = new User(name, password);
        music = PlayerPrefs.HasKey(UserMusic_Key) ? PlayerPrefs.GetInt(UserMusic_Key) : 1;
        sound = PlayerPrefs.HasKey(UserSound_Key) ? PlayerPrefs.GetInt(UserSound_Key) : 1;
    }

    private void Write()
    {
        PlayerPrefs.SetString(UserName_Key, player.userName);
        PlayerPrefs.SetString(UserPassword_Key, player.userPassword);
        PlayerPrefs.SetInt(UserMusic_Key, music);
        PlayerPrefs.SetInt(UserSound_Key, sound);
    }

    public bool HavePlayerCache()
    {
        if (string.IsNullOrEmpty(player.userName)||string.IsNullOrEmpty(player.userPassword))
        {
            return false;
        }
        return true;
    }
}
public struct User
{
    public string userName;
    public string userPassword;
    public User(string _name,string _password)
    {
        this.userName = _name;
        this.userPassword = _password;
    }
}
